/**
 * Created by kangli on 2018/4/9.
 */
//浙江精神这本书6
var sum;
var counts='';
var pageThis =0;
var pagecount = 5;
var index = 0;
var type = JSON.parse(sessionStorage.getItem("type1")) || 5;
var bookid='';
/*$.ajaxSetup({ async: false });*/

$(function () {

    getCourseName();

    bookid = $("#chooseTest").children("option:selected").attr("data-typeid");
    //判断5种课的不同
 /*   if(type=="0"||type=="1"||type=="2"||type=="3"){
        $(".pl-leftT").text("章节目录");
        parent.$(".changeTest").attr("href","../poolMana/poolYjskc.html");
    }else if(type=="4"){
        parent.$(".changeTest").attr("href","../poolMana/poolGgzyk.html");
    }else if(type=="5"){
        parent.$(".changeTest").attr("href","../poolMana/poolZjjs.html");
    }else if(type=="6"){
        $(".pl-leftT").text("教材大纲");
        parent.$(".changeTest").attr("href","../poolMana/poolYjskc.html");
    }*/

    $("#chooseTest").change(function(){
        var p1=$(this).children('option:selected').attr("data-id");
        if(p1=="0"||p1=="1"||p1=="2"||p1=="3"||p1=="6"){
            sessionStorage.setItem("type1",JSON.stringify(p1));
            self.location.href='poolYjskc.html'
        }else if(p1=="4"){
            sessionStorage.setItem("type1",JSON.stringify(p1));
            self.location.href='poolGgzyk.html'
        }else if(p1=="5"){
            sessionStorage.setItem("type1",JSON.stringify(p1));
            self.location.href='poolZjjs.html'
        }
    })
    //得到浙江新闻
 /*   getZhejiangNews();*/
    //得到理论研究
    getLilunYanjiu()
    //得到实践案例
    getPractiseExpample();
    //得到红船精神
    getHongBoat();
    //得到浙江视屏
    getZhejiangVideo();

    $('#editTestT').on('shown.bs.modal', function () {
        $(".bookName").val($("#chooseTest").children("option:selected").text())
    })

    $(".content_left").css("height",$(".content_right").height());



});

//增加素材
function addresource(btn){
    console.log(bookid);
    $("#addFile").modal("toggle");
    $(".easy_upload_upbtn").show();
    $('#easyContainer').easyUpload({
        allowFileTypes: '*',//允许上传文件类型，格式';*.doc;*.pdf'
        allowFileSize: 2000000,//允许上传文件大小(KB)
        selectText: '选择文件',//选择文件按钮文案
        multi: false,//是否允许多文件上传
        multiNum: 5,//多文件上传时允许的文件数
        showNote: false,//是否展示文件上传说明
        note: '提示：最多上传5个文件，支持格式为doc、pdf、jpg',//文件上传说明
        showPreview: false,//是否显示文件预览
        url: basePath+'/super/common/zhejiang/addCommonResource',//上传文件地址
        fileName: 'file',//文件filename配置参数
        formParam: {
            bookId: bookid,
            type: 'ZHE_JIANG_VIDEO'
        },//文件filename以外的配置参数，格式：{key1:value1,key2:value2}
        timeout: 6000000,//请求超时时间
        okCode: 1000,//与后端返回数据code值一致时执行成功回调，不配置默认200
        successFunc: function(res) {
            index=$("#zjShiPingPage").find("li").find(".active").find("a").text()-1;
            if(index!=-1&&index!=null&&index!=undefined&&index!=""){
                getZhejiangVideo();
            }else{
                index=0;
                getZhejiangVideo();
            }
        },//上传成功回调函数
        errorFunc: function(res) {
        },//上传失败回调函数
        deleteFunc: function(res) {

        }//删除文件回调函数
    })
}

//改名字
function editBookName(btn){
    var newName = $(".bookName").val();
    var  data={bookId:bookid,bookName: newName};
    console.log(data);
    AjaxSubmit("POST",data, basePath + "/super/common/zhejiang/updateBookName",updateBookName_fun);
    $("#editTestT").modal("hide");
}
function updateBookName_fun(res){
    console.log(res);
    if(res.status=='SUCCESS'){
        getCourseName();
    }else{
    }
}

//获取书名
function getCourseName(){
    var  data={};
    AjaxSubmit2("GET",data, basePath + "/super/common/zhejiang/listBook",listBook_fun);
}
function listBook_fun(res){
   /* console.log(res);*/
    if(res.status=='SUCCESS') {
        var bookLists = res.obj;
        if (bookLists.length != 0) {
            $("#chooseTest").empty();
            $.each(bookLists, function (i, bookList) {
                if(bookList.useType!="THE_USE_OF_THE_TEACHING_MATERIALS_IN_THE_ELECTIVE_COURSE_OF_IDEOLOGICAL_AND_POLITICAL_AFFAIRS") {
                   if(i==type){
                       $("#chooseTest").append('<option data-typeid='+bookList.id+' data-id="'+i+'" selected>'+bookList.bookName+'</option>');
                   }else{
                       $("#chooseTest").append('<option data-typeid='+bookList.id+' data-id="'+i+'">'+bookList.bookName+'</option>');
                   }

                }
            })
        }
    }
}
//浙江新闻的新闻id为1
/*function getZhejiangNews(){
    var  data={page: index ,size: pagecount,commonNewsTypeId:1,bookId:bookid};
    console.log(data);
    AjaxSubmit("GET",data, basePath + "/super/common/zhejiang/searchCommonNews",searchCommonNews_fun);
}

function searchCommonNews_fun(res){
    console.log(res);
    if(res.status=='SUCCESS'){
        var newsLists = res.obj.content;
        sum = res.obj.totalElements;
        counts = sum / pagecount;
        if (newsLists.length != 0) {
            $("#zhejiangPage").css("display","block");
            if (sum % pagecount == 0) {
                counts = parseInt(sum / pagecount);
            }
            else {
                counts = parseInt(sum / pagecount) + 1;
            }
            $("#zhejiangPage").paging({
                pageNo: pageThis,
                totalPage: counts,
                totalSize: sum,
                callback: function (num) {
                    console.log(num);
                    index = num - 1;
                    getZhejiangNews();
                    pageThis = num;
                }
            });
            $(".zhejiangTbody").empty();
            $('.warnDiv').remove();
            $.each(newsLists, function (i, newsList) {
                var zhaiyao = newsList.abstructContent || '';
                var createTime = newsList.creatTime || '';
                var statustr = '';
                if(newsList.status='DOWN'){//未发布
                    statustr='未发布 |<span class="cursorP colorBlue" data-newstypeId="1" data-id="'+newsList.id+'" onclick="changeStatu(this)"> 发布</span>'
                }else if(newsList.status='UP'){ //已发布
                    statustr='已发布|<span class="cursorP colorBlue" data-newstypeId="1" data-id="'+newsList.id+'"  onclick="changeStatu2(this)"> 关闭</span>'
                }
                var html = '<tr> ' +
                    '<td>'+newsList.title+'</td> ' +
                '<td>'+zhaiyao+'</td> ' +
                '<td>'+createTime+'</td> ' +
                '<td>'+statustr+'</span></td> ' +
                    '<td class="texC ">  <a   class="texC  cursorP colorBlue" href="../informationMana/addNews.html?type=commonNews&commonNewsTypeId=1&bookId='+bookid+'&needUpdate=1&nid='+newsList.id+'">编辑</a> | <span class=" cursorP colorBlue" data-id="'+newsList.id+'" onclick="deleteNews(this)" data-newstypeId="1">删除</span></td> ' +
                    '</tr>';
                $(".zhejiangTbody").append(html)
            })

        }
        else{
            $("#zhejiangPage").css("display","none");
            $(".zhejiangTbody").empty();
            $('.warnDiv').remove();
            var html = $('<div class="text-center warnDiv "  style="width: 100%;margin: 73px 0px;font-size: 14px" >无信息!</div>');
            $(".zhejiangTable").after(html)
        }
    }
}*/

function getLilunYanjiu(){
    var  data={page: index ,size: pagecount,commonNewsTypeId:2,bookId:bookid};
    AjaxSubmit("GET",data, basePath + "/super/common/zhejiang/searchCommonNews",searchCommonNews_fun1);
}
function searchCommonNews_fun1(res){
    if(res.status=='SUCCESS'){
        var newsLists = res.obj.content;
        sum = res.obj.totalElements;
        counts = sum / pagecount;
        if (newsLists.length != 0) {
            $("#lilunPage").css("display","block");
            if (sum % pagecount == 0) {
                counts = parseInt(sum / pagecount);
            }
            else {
                counts = parseInt(sum / pagecount) + 1;
            }
            $("#lilunPage").paging({
                pageNo: pageThis,
                totalPage: counts,
                totalSize: sum,
                callback: function (num) {
                    console.log(num);
                    index = num - 1;
                    getLilunYanjiu();
                    pageThis = num;
                }
            });
            $(".lilunTbody").empty();
            $('.warnDiv2').remove();
            $.each(newsLists, function (i, newsList) {
                var zhaiyao = newsList.abstructContent || '';
                var createTime = newsList.creatTime || '';
                var statustr = '';
                if(newsList.status=='DOWN'){//未发布
                    statustr='未发布 |<span class="cursorP colorBlue" data-newstypeId="2" data-id="'+newsList.id+'"  onclick="changeStatu(this)"> 发布</span>'
                }else if(newsList.status=='UP'){ //已发布
                    statustr='已发布|<span class="cursorP colorBlue" data-newstypeId="2" data-id="'+newsList.id+'"  onclick="changeStatu2(this)"> 关闭</span>'
                }
                var html = '<tr> ' +
                    '<td>'+newsList.title+'</td> ' +
                    '<td>'+zhaiyao+'</td> ' +
                    '<td>'+createTime+'</td> ' +
                    '<td>'+statustr+'</span></td> ' +
                    '<td class="texC ">  <a   class="texC  cursorP colorBlue" href="../informationMana/addNews.html?type=commonNews&commonNewsTypeId=2&bookId='+bookid+'&needUpdate=1&nid='+newsList.id+'">编辑</a> | <span class=" cursorP colorBlue" data-id="'+newsList.id+'" data-newstypeId="2" onclick="deleteNews(this)" >删除</span></td> ' +
                    '</tr>';
                $(".lilunTbody").append(html)
            })

        }
        else{
            $("#lilunPage").css("display","none");
            $(".lilunTbody").empty();
            $('.warnDiv2').remove();
            var html = $('<div class="text-center warnDiv2 "  style="width: 100%;margin: 73px 0px;font-size: 14px" >无信息!</div>');
            $(".lilunTable").after(html)
        }
    }
}

function getPractiseExpample(){
    var  data={page: index ,size: pagecount,commonNewsTypeId:3,bookId:bookid};
    AjaxSubmit("GET",data, basePath + "/super/common/zhejiang/searchCommonNews",searchCommonNews_fun3);
}
function searchCommonNews_fun3(res){
    if(res.status=='SUCCESS'){
        var newsLists = res.obj.content;
        sum = res.obj.totalElements;
        counts = sum / pagecount;
        if (newsLists.length != 0) {
            $("#shijianPage").css("display","block");
            if (sum % pagecount == 0) {
                counts = parseInt(sum / pagecount);
            }
            else {
                counts = parseInt(sum / pagecount) + 1;
            }
            $("#shijianPage").paging({
                pageNo: pageThis,
                totalPage: counts,
                totalSize: sum,
                callback: function (num) {
                    console.log(num);
                    index = num - 1;
                    getPractiseExpample();
                    pageThis = num;
                }
            });
            $(".shijianTbody").empty();
            $('.warnDiv3').remove();
            $.each(newsLists, function (i, newsList) {
                var zhaiyao = newsList.abstructContent || '';
                var createTime = newsList.creatTime || '';
                var statustr = '';
                if(newsList.status=='DOWN'){//未发布
                    statustr='未发布 |<span class="cursorP colorBlue" data-newstypeId="3" data-id="'+newsList.id+'"  onclick="changeStatu(this)"> 发布</span>'
                }else if(newsList.status=='UP'){ //已发布
                    statustr='已发布|<span class="cursorP colorBlue" data-newstypeId="3" data-id="'+newsList.id+'"  onclick="changeStatu2(this)"> 关闭</span>'
                }
                var html = '<tr> ' +
                    '<td>'+newsList.title+'</td> ' +
                    '<td>'+zhaiyao+'</td> ' +
                    '<td>'+createTime+'</td> ' +
                    '<td>'+statustr+'</span></td> ' +
                    '<td class="texC ">  <a   class="texC  cursorP colorBlue" href="../informationMana/addNews.html?type=commonNews&commonNewsTypeId=3&bookId='+bookid+'&needUpdate=1&nid='+newsList.id+'">编辑</a> | <span class=" cursorP colorBlue" data-id="'+newsList.id+'" data-newstypeId="3" onclick="deleteNews(this)" >删除</span></td> ' +
                    '</tr>';
                $(".shijianTbody").append(html)
            })

        }
        else{
            $("#shijianPage").css("display","none");
            $(".shijianTbody").empty();
            $('.warnDiv3').remove();
            var html = $('<div class="text-center warnDiv3 "  style="width: 100%;margin: 73px 0px;font-size: 14px" >无信息!</div>');
            $(".shijianTable").after(html)
        }
    }
}

function getHongBoat(){
    var  data={page: index ,size: pagecount,commonNewsTypeId:4,bookId:bookid};
    AjaxSubmit("GET",data, basePath + "/super/common/zhejiang/searchCommonNews",searchCommonNews_fun4);
}
function searchCommonNews_fun4(res){
    //console.log(res);
    if(res.status=='SUCCESS'){
        var newsLists = res.obj.content;
        sum = res.obj.totalElements;
        counts = sum / pagecount;
        if (newsLists.length != 0) {
            $("#hcPage").css("display","block");
            if (sum % pagecount == 0) {
                counts = parseInt(sum / pagecount);
            }
            else {
                counts = parseInt(sum / pagecount) + 1;
            }
            $("#hcPage").paging({
                pageNo: pageThis,
                totalPage: counts,
                totalSize: sum,
                callback: function (num) {
                    console.log(num);
                    index = num - 1;
                    getHongBoat();
                    pageThis = num;
                }
            });
            $(".hcTbody").empty();
            $('.warnDiv4').remove();
            $.each(newsLists, function (i, newsList) {
                var createTime = newsList.creatTime || '';
                var html = '  <tr> ' +
                    '<td>'+newsList.title+'</td> ' +
                    '<td>'+createTime+'</td> ' +
                    '<td class="texC colorBlue"><span class=" cursorP colorBlue" data-id="'+newsList.id+'" data-newstypeId="4" onclick="deleteNews(this)" >删除</span></tr>';
                $(".hcTbody").append(html)
            })

        }
        else{
            $("#hcPage").css("display","none");
            $(".hcTbody").empty();
            $('.warnDiv4').remove();
            var html = $('<div class="text-center  warnDiv4"  style="width: 100%;margin: 73px 0px;font-size: 14px" >无信息!</div>');
            $(".hcTable").after(html)
        }
    }
}

function getZhejiangVideo(){
    var  data={page: index ,size: pagecount,type:"ZHE_JIANG_VIDEO",bookId:bookid};
    //console.log(data);
    AjaxSubmit("GET",data, basePath + "/super/common/zhejiang/searchCommonResource",searchCommonResource_fun5);
}
function searchCommonResource_fun5(res){
    console.log(res);
    if(res.status=='SUCCESS'){
        var fileLists = res.obj.content;
        sum = res.obj.totalElements;
        counts = sum / pagecount;
        if (fileLists.length != 0) {
            $("#zjShiPingPage").css("display","block");
            if (sum % pagecount == 0) {
                counts = parseInt(sum / pagecount);
            }
            else {
                counts = parseInt(sum / pagecount) + 1;
            }
            $("#zjShiPingPage").paging({
                pageNo: pageThis,
                totalPage: counts,
                totalSize: sum,
                callback: function (num) {
                    console.log(num);
                    index = num - 1;
                    getZhejiangVideo();
                    pageThis = num;
                }
            });
            $(".zjShiPingTbody").empty();
            $('.warnDiv5').remove();
            $.each(fileLists, function (i, fileList) {
                var number = fileList.number ||''
                var html = '  <tr> ' +
                    '<td>'+number+'</td> ' +
                    '<td>'+fileList.title+'</td> ' +
                    '<td class="texC colorBlue"><span class=" cursorP" data-type="ZHE_JIANG_VIDEO" onclick="deleteResource(this)" data-id="'+fileList.id+'">删除</span></td> </tr>';
                $(".zjShiPingTbody").append(html)
            })

        }
        else{
            $("#zjShiPingPage").css("display","none");
            $(".zjShiPingTbody").empty();
            $('.warnDiv5').remove();
            var html = $('<div class="text-center  warnDiv5"  style="width: 100%;margin: 73px 0px;font-size: 14px" >无信息!</div>');
            $(".zjShiPingTable").after(html)
        }
    }
}

function deleteNews(btn){
    var msg = "您真的确定要删除吗？\n\n请确认！";
    if (confirm(msg)==true){
        var newsId = $(btn).attr("data-id");
        var  data={commonNewsId: newsId};
        var newsTypeId = $(btn).attr("data-newstypeId");
        AjaxSubmit("POST",data, basePath + "/super/common/zhejiang/deleteCommonNews",deleteCommonNews_fun);
        function deleteCommonNews_fun(res){
       /*     console.log(res);*/
            if(res.status=='SUCCESS'){
               /* index = $(btn).parent().parent().parent().parent().next().find(".pagination").find(".active").find("a").text()-1;*/
                index=0;
                if(newsTypeId==1){
                    getZhejiangNews();
                }else if(newsTypeId==2){
                    getLilunYanjiu()
                }else if(newsTypeId==3){
                    getPractiseExpample();
                }else if(newsTypeId==4){
                    getHongBoat();
                }
            }
        }
    }else{
        return false;
    }

}

function deleteResource(btn){
    var msg = "您真的确定要删除吗？\n\n请确认！";
    if (confirm(msg)==true){
        var newsId = $(btn).attr("data-id");
        var  data={commonResourceId: newsId};
        var type = $(btn).attr("data-type");
        AjaxSubmit("POST",data, basePath + "/super/common/zhejiang/deleteCommonResource",deleteCommonResource_fun);
        function deleteCommonResource_fun(res){
            /*     console.log(res);*/
            if(res.status=='SUCCESS'){
              /*  index = $(btn).parent().parent().parent().parent().next().find(".pagination").find(".active").find("a").text()-1;*/
                index=0;
                if(type=="ZHE_JIANG_VIDEO"){
                    getZhejiangVideo();
                }
            }
        }
    }else{
        return false;
    }
}

//改变状态UP
function changeStatu(btn){
    var newsId = $(btn).attr("data-id");
    var  data={commonNewsId: newsId,status:"UP"};
    var newsTypeId = $(btn).attr("data-newstypeId");
    AjaxSubmit("POST",data, basePath + "/super/common/zhejiang/changeCommonNewsStatus",changeCommonNewsStatus_fun);
    function changeCommonNewsStatus_fun(res){
        if(res.status=='SUCCESS'){
            index = $(btn).parent().parent().parent().parent().next().find(".pagination").find(".active").find("a").text()-1;
            if(newsTypeId==2){
                getLilunYanjiu();
            }else if(newsTypeId==3){
                getPractiseExpample();
            }
        }
    }
}

//改变状态DOWN
function changeStatu2(btn){
    var newsId = $(btn).attr("data-id");
    var  data={commonNewsId: newsId,status:"DOWN"};
    var newsTypeId = $(btn).attr("data-newstypeId");
    AjaxSubmit("POST",data, basePath + "/super/common/zhejiang/changeCommonNewsStatus",changeCommonNewsStatus_fun);
    function changeCommonNewsStatus_fun(res){
        if(res.status=='SUCCESS'){
            index = $(btn).parent().parent().parent().parent().next().find(".pagination").find(".active").find("a").text()-1;
           if(newsTypeId==2){
                getLilunYanjiu();
            }else if(newsTypeId==3){
                getPractiseExpample();
            }
        }
    }
}
function getTime() {
    setTimeout(function () {
        var duration = $("#video")[0].duration;
        if(isNaN(duration)){
            getTime();
        }
        else{
            console.info("该歌曲的总时间为："+$("#video")[0].duration+"秒");
        }
    }, 10);
}

<!--把文件转换成可读URL-->
function getObjectURL(file) {
    var url = null;
    if (window.createObjectURL != undefined) { // basic
        url = window.createObjectURL(file);
    } else if (window.URL != undefined) { // mozilla(firefox)
        url = window.URL.createObjectURL(file);
    } else if (window.webkitURL != undefined) { // webkit or chrome
        url = window.webkitURL.createObjectURL(file);
    }
    return url;
}



